package com;

import com.intelligt.modbus.jlibmodbus.utils.DataUtils;

public class ExampleHexToString {
    static public void main(String[] argv) {

        byte[] bytes = new byte[]{0x41, 0x42};
        StringBuffer aa = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            char c = (char) bytes[i];
            aa.append(c);
        }
        System.out.println("value:" + aa);

        System.out.println("frame recv " + DataUtils.toAscii(bytes));
        String value = toHex(bytes, 0, 1);
        System.out.println(value);

    }

    public static final String toHex(byte[] data, int off, int length) {
        // double size, two bytes (hex range) for one byte
        StringBuffer buf = new StringBuffer(data.length * 2);
        for (int i = off; i < length; i++) {
            // don't forget the second hex digit
            if (((int) data[i] & 0xff) < 0x10) {
                buf.append("0");
            }
            buf.append(Long.toString((int) data[i] & 0xff, 16));
            if (i < data.length - 1) {
                buf.append(" ");
            }
        }
        return buf.toString();
    }
}
